package com.example.order.service.impl;



import com.example.common.result.PageBean;
import com.example.model.customer.pojo.Customer;
import com.example.model.order.dto.OrdersCustonmerDTO;
import com.example.model.order.dto.OrdersDTO;
import com.example.order.mapper.OrdersMapper;
import com.example.order.service.OrdersService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
@Service
public class OrdersServiceimpl implements OrdersService {
    @Autowired
    private OrdersMapper ordersMapper;

    @Override
    public void delete(List<Integer> ids) {
            ordersMapper.delete(ids);
    }

    /**
     * 保存订单和顾客表
     * @param ordersCustonmerDTO
     */
    @Transactional//保证原子性
    @Override
    public void save(OrdersCustonmerDTO ordersCustonmerDTO) {
        //1保存客户表
        Customer customer = new Customer();
        ordersMapper.saveCustomer(ordersCustonmerDTO);
        //2返回id保存
        int custId = customer.getCustomerId();
        ordersCustonmerDTO.setOrderId(custId);
        //3保存订单
        ordersMapper.saveOrders(ordersCustonmerDTO);
    }
    @Override
    public OrdersCustonmerDTO selectById(Integer id) {
        return ordersMapper.selectById(id);
    }

    /**
     * 更新
     * @param ordersCustonmerDTO
     */
    @Transactional
    @Override
    public void updateById(OrdersCustonmerDTO ordersCustonmerDTO) {
        //1更新Custonmer表
        Customer customer = new Customer();
        BeanUtils.copyProperties(ordersCustonmerDTO,customer);
        ordersMapper.updateCustonmerById(customer);
        //2更新orders表
        ordersMapper.updateOrdersById(ordersCustonmerDTO);
    }

    @Override
    public PageBean page(Integer page, Integer pageSize, LocalDateTime orderDateStart, LocalDateTime orderDateEnd, String shipmentAddress, String CustomerName) {
        PageHelper.startPage(page, pageSize);
        List<OrdersDTO> list= ordersMapper.page(orderDateStart,orderDateEnd,shipmentAddress,CustomerName);
        Page<OrdersDTO> p = (Page<OrdersDTO>) list;
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
        return pageBean;
    }

}
